package com.cdream.basic.algorithm.leetcode.stack;

import java.util.LinkedList;

/**
 * @author cdream
 * @date 6/26/21
 */
public class Question71 {
    public String simplifyPath(String path) {
        String[] split = path.split("/");
        LinkedList<String> list = new LinkedList<>();
        for (String s : split) {
            if ("".equals(s)||s==null||".".equals(s)){
                continue;
            }
            if ("..".equals(s)){
                if(!list.isEmpty()){
                    list.pop();

                }
                continue;

            }
            list.push(s);
        }
        if(list.isEmpty()){
            return "/";
        }
        StringBuilder sb = new StringBuilder();
        while(!list.isEmpty()){
            sb.append("/").append(list.removeLast());
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        LinkedList<Integer> integers = new LinkedList<>();
        integers.push(1);
        integers.push(2);
        integers.push(3);
        System.out.println(integers);
        System.out.println(integers.pop());
        System.out.println(integers.pop());
        System.out.println(integers.pop());
    }
}
